<?php

use addons\epay\library\Service;

function getAppIdAndSecret()
{
    $appid = config('site.miniapp_id');
    $app_secret = config('site.mini_secret');
    return array($appid, $app_secret);
}

/**
 * Account:获取token
 * User: Ixiangang
 * Date: 2025/1/11
 * Time: 09:35
 */
function getAccessToken()
{
    list($appid, $app_secret) = getAppIdAndSecret();
    $url = "https://api.weixin.qq.com/cgi-bin/stable_token";
    $data = [
        'grant_type' => 'client_credential',
        'appid' => $appid,
        'secret' => $app_secret,
    ];
    $result = httpPost($url, $data);
    if (isset($result['access_token']) && !empty($result['access_token'])) {
        return $result['access_token'];
    } else {
        throw_validate_error(isset($result['errcode']), $result['errmsg']);
    }
}

/**
 * Account:小程序手机号
 * User: Ixiangang
 * Date: 2025/1/11
 * Time: 09:36
 */
function getWxPhone(string $wechatCode)
{
    throw_validate_error(!$wechatCode, "缺少微信手机号code错误");
    $access_token = getAccessToken();
    $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" . $access_token;
    $data = [
        'code' => $wechatCode
    ];
    $res = httpPost($url, $data);
    if (!empty($res) && $res['errcode'] == 0) {
        return $res['phone_info']['phoneNumber'];
    } else {
        throw_validate_error($res['errcode'] != 0, $res['errmsg']);
    }
}

/**
 * @param $code
 * @return mixed|void
 * @获取小程序openid
 */
function getOpenId($code)
{
    list($appid, $app_secret) = getAppIdAndSecret();
    $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$app_secret&js_code=$code&grant_type=authorization_code";
    $result = json_decode(file_get_contents($url), true);
    if (isset($result['openid']) && !empty($result['openid'])) {
        return $result['openid'];
    } else {
        throw_validate_error($result['errcode'] != 0, $result['errmsg']);
    }
}


/**
 * 以post方式发起请求
 * @param $url
 * @param $data
 * @return bool|mixed
 */
function httpPost($url, $data)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_POST, true);

    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_UNICODE));
    list($content, $status) = array(curl_exec($curl), curl_getinfo($curl), curl_close($curl));
    return (intval($status["http_code"]) === 200) ? json_decode($content, true) : false;
}


/**
 * Account:退款
 * User: Ixiangang
 * Date: 2025/2/13
 * Time: 10:29
 */
function refundOrder($order_no, $pay_type, $money, $com = "退款")
{
    $config = Service::getConfig($pay_type);
    if ($pay_type == 'alipay') {
        $refund_order = [
            'out_trade_no' => $order_no,
            'out_request_no' => time(),//退款订单号
            'refund_amount' => $money,
        ];
        $pay = \Yansongda\Pay\Pay::alipay($config)->refund($refund_order);
        return $pay;
    } else {
        $refund_order = [
            'out_trade_no' => $order_no ?? '',
            'amount' => $money * 100 ?? 0,
            'refund_fee' => $money * 100 ?? 0,
            'total_fee' => $money * 100 ?? 0,
            'type' => 'app',
            'out_refund_no' => time(),
            'refund_reason' => $com
        ];
        $pay = \Yansongda\Pay\Pay::wechat($config)->refund($refund_order);
        return $pay;
    }


}









